clear
*1940
do "usa_00115.do"
clear
*1920
do "usa_00086.do"

clear
use "1920_full.dta"

*Exclude people living in group quarters
keep if gq~=3 & gq~=4

local vars occscore 
foreach x of local vars{
gen max`x'parent = max(occscore_mom, occscore_pop)
}

keep histid age sex maxoccscoreparent race stateicp countyicp occ1950_pop occ1950_mom

* rename the variables to indicate the year they are from
for var histid age sex maxoccscoreparent race stateicp countyicp occ1950_pop occ1950_mom: rename X X1920

*Merge with Eckert crosswalk
rename stateicp1920 icpsrst 
rename countyicp1920 icpsrcty
	
joinby icpsrst icpsrcty using "EGLP_1920.dta", unmatched(both)

rename _merge merge1920crosswalk

gen cty_fips = nhgisst_1990*100 + (nhgiscty_1990/10)

*Drop non-merged from crosswalk
drop if merge1920crosswalk==2

*There are some counties from IPUMS that don't merge to the crosswalk.  None of these affect normal/asylum counties, so can drop them
*See parent_occ1920.do for details.
drop if merge1920crosswalk==1

merge m:1 cty_fips using "justnormasylum.dta"
rename _merge mergenormasylum

*Keep normal school and asylum counties
keep if mergenormasylum==3

rename cty_fips cty_fips1920
drop year icpsrst icpsrcty nhgisst_1990 nhgiscty_1990 nhgisst nhgiscty statenam nhgisnam area_base statenam_1990 nhgisnam_1990 icpsrst_1990 icpsrcty_1990 area us_state 
for var hasnormalschool hasnormalorasylum mergenormasylum: rename X X1920

tostring cty_fips1920, gen(cty_fipstr)
gen statefip1920 = substr(cty_fipstr, 1,1) if length(cty_fipstr)==4
replace statefip1920 = substr(cty_fipstr, 1, 2) if length(cty_fipstr)==5
destring statefip1920, replace

*Drop DE, Georgia, RI, SC, NV, WY, AZ, NM, AK, HI because don't have at least one normal school and one asylum county

drop if statefip==2|statefip==4|statefip==10|statefip==13|statefip==15|statefip==32|statefip==35|statefip==44|statefip==45|statefip==56

save "1920_small.dta", replace


use "1940_full.dta", clear

keep histid age sex stateicp countyicp wkswork2 durunemp

* rename the variables to indicate the year they are from
for var histid age sex  stateicp countyicp wkswork2 durunemp: rename X X1940

save "1940_small.dta", replace

clear
*Merging the IPUMS censuses to the Census Tree crosswalks 
import delimited using "1920_1940.csv", varnames(1)
save "1920_1940.dta", replace

merge 1:m histid1920 using "1920_small.dta"
rename _merge mergetree1920

gen merged1920 = mergetree1920==3
drop if mergetree1920==1


gen male1920 = sex1920==1
replace male1920 = . if sex1920==.

gen white1920 = race1920==1
replace white1920 = . if race1920==.

gen black1920 = race1920==2
replace black1920 = . if race1920==.

gen occparentsnonmiss = (occ1950_pop1920~=.|occ1950_mom1920~=.)

*Only keep those that are living with at least one of their parents--to avoid getting people who traveled for school, asylum

keep if occparentsnonmiss==1

egen taghistid1920 =tag(histid1920)
tab taghistid1920

keep if mergetree1920==3

merge m:1 histid1940 using "1940_small.dta"
rename _merge mergetree1940
drop if mergetree1940==2

drop if mergetree1940==1

gen worked40weeks1940 = wkswork21940>=4

gen unemp65wks1940 = durunemp1940>=65
replace unemp65wks1940 = . if durunemp1940==998|durunemp1940==999

gen overlap1940 = worked40weeks1940==1 & unemp65wks1940==1
replace overlap1940 = . if unemp65wks1940==.

gen constant = 1

gen bmedmaxoccscoreparent1920 = maxoccscoreparent1920<=20 if white1920==1
replace bmedmaxoccscoreparent1920 = maxoccscoreparent1920<=20 if white1920==0

save "1920_1940_merged_workrelief.dta", replace

*By parental occupation score

forvalues i = 0(1)1{
preserve
keep if (white1920==1|black1920==1)

keep if bmedmaxoccscoreparent1920==`i'

#delimit ;
local vars constant worked40weeks1940 overlap1940 
;
foreach x of local vars{;
gen `x'wt = `x'*weight;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) constant overlap1940 worked40weeks1940 (mean) statefip1920 hasnormalschool, by(cty_fips1920 white1920 male1920);


#delimit cr

gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool white1920 male1920: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool white1920 male1920: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip white1920 male1920: egen maxfracbelow10 = max(fracbelow10)

tab statefip if maxfracbelow10>=.5
drop if maxfracbelow10>=.5

gen overlap1940ftmean = overlap1940/worked40weeks1940

sort cty_fips
local vars overlap1940ft
foreach x of local vars{

reghdfe `x'mean hasnormalschool1920 if white1920==1 & male1920==1, absorb(statefip1920) cluster(statefip1920)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1
outreg2 using censustree19201940.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

reghdfe `x'mean hasnormalschool1920 if white1920==1 & male1920==0, absorb(statefip1920) cluster(statefip1920)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1
outreg2 using censustree19201940.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

reghdfe `x'mean hasnormalschool1920 if white1920==0 & male1920==1, absorb(statefip1920) vce(robust)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1
outreg2 using censustree19201940bl.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

#delimit ;
permute hasnormalschool _b[hasnormalschool], strata(statefip1920) reps(1000): reg `x'mean hasnormalschool i.statefip1920 if white1920==0 & male1920==1;

#delimit cr

reghdfe `x'mean hasnormalschool1920 if white1920==0 & male1920==0, absorb(statefip1920) vce(robust)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1
outreg2 using censustree19201940bl.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)
#delimit ;
permute hasnormalschool _b[hasnormalschool], strata(statefip1920) reps(1000): reg `x'mean hasnormalschool i.statefip1920 if white1920==0 & male1920==0;

#delimit cr
}
restore
}
